function gameSet(obj) {
	this.music = ['/music/1.mp3','/music/2.mp3','/music/3.mp3'];
	this.aud = obj;
	this.currentIndex = '';
}
gameSet.prototype = {
	isLock:false,
	specialArr:["?","&","@","#","%","/",'"',"'","？"],
	init:function() {

	},
	getElem:function(id) {
		return document.getElementById(id);
	},
	//选中
	checkRadio:function(that) {
		var that = that;
			that.parent().parents(".row").find(".radio").removeClass("icon-xuanzhong");
			that.addClass("icon-xuanzhong");
	},
	//试听
	tryListen:function(that,index) {
		var that = that;
			if(that.hasClass("try")) {
				that.removeClass("try");
			}else {
				that.parent().parents(".fl").find(".music").removeClass("try");
				that.addClass("try");
			}
			this.player(index);
		this.currentIndex = index;
	},
	// 判断是播放还是停止
	player:function(index) {
		obj = this.aud;
		if(index === this.currentIndex && !obj.paused) {
			obj.pause();
			return;
		}
		var src = this.music[index];
		obj.src = src;
		if(obj.paused){
			// 播放
            obj.play();
        }else{
        	// 暂停
            obj.pause();
        }
	},
	CheckCode:function (str) {
	    var arr = this.specialArr;
	    var len = arr.length;
	    for (var i = 0; i < len; i++) {
	    	if(str.indexOf(arr[i]) >= 0) {
	    		return false;
	    	}
	    }
	    return true;
	},
	isEmpty:function(s) {
		var flag = s.length > 0 ? false : true;
		return flag;
	},
	//提交数据
	_commit:function(data,index) {
		this.isLock = true;
		data.src = this.music[index];
		$.ajax({
			type:"post",
			url:"/tug/BhDetails/UpdateDetails",
			dataType:"json",
			data: data,
			success:function(ret) {
				  var msg = ret;
				if(msg.reCode == 0){
					msgBox.showMsgbox('#box-success',"保存成功",'','');
				}else {
					msgBox.showMsgbox('#box-fail',"保存失败");
				}
				this.isLock = false;
			},
			error:function() {
                this.isLock = false;
			}
		})
	}
}
$(function() {
	var gameset = new gameSet(document.getElementById("aud"));
	//radio切换
	$("#tb").find(".radio").on("click",function() {
		gameset.checkRadio($(this));
	});

	//试听
	$("#tb").find(".music").on("click",function() {
		var index = $("#tb").find(".music").index($(this));
			gameset.tryListen($(this),index);
	});

	//保存
	$("#save").on("click",function() {
		var A_name = $.trim($("#A-name").val());
		if(gameset.isEmpty(A_name)){
			msgBox.showMsgbox('#box-warning',"队名不能为空",'','');
			return;
		}
		if(!gameset.CheckCode(A_name)){
			msgBox.showMsgbox('#box-warning',"队名不能含有特殊字符",'','');
			return;
		}
		if(A_name.length > 5) {
			msgBox.showMsgbox('#box-warning',"队名最多5个字！",'','');
			return;
		}
		var B_name = $.trim($("#B-name").val());
		if(gameset.isEmpty(B_name)){
			msgBox.showMsgbox('#box-warning',"队名不能为空",'','');
			return;
		}
		if(!gameset.CheckCode(B_name)){
			msgBox.showMsgbox('#box-warning',"队名不能含有特殊字符",'','');
			return;
		}
		if(B_name.length > 5) {
			msgBox.showMsgbox('#box-warning',"队名最多5个字！",'','');
			return;
		}

		// 游戏时间
		var time = $("#time").find(".icon-xuanzhong").attr("data-value");
		// 上限人数
		var number = $("#number").find(".icon-xuanzhong").attr("data-value");

		var check_music = $("#bg-music").find(".icon-xuanzhong");
		//背景音乐索引
		var index = $("#bg-music").find(".radio").index(check_music);
		//游戏轮数
		var round = $("#round").val();

		var teamData = {};
		var obj = $("#team-logo");
			teamData.A_img = obj.find(".img_src_A").val();
			teamData.B_img = obj.find(".img_src_B").val();
			teamData.A_id = $("#A-id").val();
			teamData.B_id = $("#B-id").val();
			teamData.A_name = A_name;
			teamData.B_name = B_name;
		var data = {
					"playtime":time,
					"resultnum":number,
					"bgmusic":parseInt(index) + 1,
					"round":round,
					"src":"",
					"teamData":teamData
				};
			gameset._commit(data,index);
	});

	//高度自适应
    var url = $.trim($("#design_url").val());
    	getDocumentHeight(url);
});


/****图片上传********/
$(function() {
	var that;
	$(".upload-tips").click(function() {
		that = $(this);
        $("#basicUploadSuccessExample").find("input[type=file]").click();
        $("#imgupload").show();
        $(".qq-upload-list li").hide();
    }).leanModal({ top: 100, closeButton: null });

    $("#lean_overlay").click(function() {
        $("#imgupload").hide();
    });
    $('#basicUploadSuccessExample').fineUploader({
        request: {endpoint: '/php.php'}
    }).on('error', function(event, id, filename, reason) {
    }).on('complete', function(event, id, filename, responseJSON){
        if(responseJSON.success==true){
        	var fireName = responseJSON.uploadName;
        	var src = '/upload/'+fireName;
	        	that.siblings(".img_src").val(src);
	        	that.siblings("img").attr("src",src);
	        	$("#imgupload,#lean_overlay").hide();
        }else {
            $(".qq-upload-status-text").html("因图片过大无法上传").css("fontWeight","bold");
        }
    });
    $(".qq-upload-button div").html("上传图片");
})